package com.whut.www.file_storage;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
 * Created by clvc on 2017/7/7.
 * 真正的速度是看不见的 !
 * Today is today , we will go !
 */

public class CustomProvider extends ContentProvider {
    //数据库名
    private static final String DB_NAME = "db_words.db";
    //数据库版本
    private static final int DB_VERSION = 1;
    //AUTHORITY提供给ContentResolver的接口,自定义的,一般为包名
    private static final String AUTHORITY = "com.palfund";
    //ContentProvider的查询路径
    private String uri_word = "content://com.palfund/tb_words";
    private static final String TABLENAME_1 = "tb_words";
    private static final String TABLENAME_2 = "tb_newwords";
    private SQLiteDatabase mSqLiteDatabase;
    private static UriMatcher mUriMatcher;

    static {
        mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        mUriMatcher.addURI(AUTHORITY, TABLENAME_1, 0);
        //"content://com.palfund/tb_words"
        mUriMatcher.addURI(AUTHORITY, TABLENAME_2, 1);
    }

    @Override
    public boolean onCreate() {
        CustomSQLiteOpenHelper sqLiteOpenHelper = new CustomSQLiteOpenHelper(getContext(),
                DB_NAME, null, DB_VERSION);
        mSqLiteDatabase = sqLiteOpenHelper.getReadableDatabase();
        //true:表示内容提供者被成功的加载.
        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String
            selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        Cursor cursor = null;
        switch (mUriMatcher.match(uri)) {
            case 0:
                cursor = mSqLiteDatabase.query(TABLENAME_1, projection, selection, selectionArgs,
                        null, null, sortOrder);
                break;
            case 1:
                cursor = mSqLiteDatabase.query(TABLENAME_2, projection, selection, selectionArgs,
                        null, null, sortOrder);
        }
        //查询后设置cursor所观察的uri,以便使用cursorLoader时自动更新UI
        cursor.setNotificationUri(getContext().getContentResolver(), Uri.parse(uri_word));
        return cursor;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        Long _id = 0L;
        switch (mUriMatcher.match(uri)) {
            case 0:
                _id = mSqLiteDatabase.insert(TABLENAME_1, null, values);

                break;
            case 1:
                _id = mSqLiteDatabase.insert(TABLENAME_2, null, values);
                break;
        }
        //通知数据库数据改变,以便CursorLoader更新UI
        getContext().getContentResolver().notifyChange(Uri.parse(uri_word), null);
        return ContentUris.withAppendedId(uri, _id);
        //"content://com.palfund/tb_words/100"
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[]
            selectionArgs) {
        int deleteCount = 0;
        switch (mUriMatcher.match(uri)) {
            case 0:
                deleteCount = mSqLiteDatabase.delete(TABLENAME_1, selection, selectionArgs);
                break;
            case 1:
                deleteCount = mSqLiteDatabase.delete(TABLENAME_2, selection, selectionArgs);
                break;
        }
        //通知数据库数据改变,以便CursorLoader更新UI
        getContext().getContentResolver().notifyChange(Uri.parse(uri_word), null);
        return deleteCount;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String
            selection, @Nullable String[] selectionArgs) {
        int updateCount = 0;
        switch (mUriMatcher.match(uri)) {
            case 0:
                updateCount = mSqLiteDatabase.update(TABLENAME_1, values, selection, selectionArgs);
                break;
            case 1:
                updateCount = mSqLiteDatabase.update(TABLENAME_2, values, selection, selectionArgs);
                break;
        }
        //通知数据库数据改变,以便CursorLoader更新UI
        getContext().getContentResolver().notifyChange(Uri.parse(uri_word), null);
        return updateCount;
    }
}
